package com.link.ppt.View;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.link.ppt.App.App;
import com.link.ppt.Base.BaseActivity;
import com.link.ppt.Model.Bean.BaseBean;
import com.link.ppt.Model.Bean.PPTBean;
import com.link.ppt.Model.Bean.PPTEntity;
import com.link.ppt.Model.IPPTModel;
import com.link.ppt.Model.Impl.PPTModelImpl;
import com.link.ppt.R;
import com.link.ppt.Utils.CommonSubscriber;
import com.link.ppt.View.Adapter.PPTStoreAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

/**
 * 收藏以及我发布的ppt画面
 * Created by liucxu on 2018/1/29.
 */

public class StorePublishActivity extends BaseActivity {

    @BindView(R.id.layout_back)
    LinearLayout back_layout;
    @BindView(R.id.tv_title)
    TextView tv_title;


    @BindView(R.id.swipe_refresh_layout)
    SmartRefreshLayout swipe_refresh_layout;
    @BindView(R.id.recycle_content)
    RecyclerView recycle_content;

    private PPTStoreAdapter adapter;
    private List<PPTEntity> datas = new ArrayList<>();

    private int page_index = 1;
    private boolean is_refresh = true;
    private boolean isMyPublishPPT = true;

    private IPPTModel pptModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_store_publish;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setupData() {
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(StorePublishActivity.this);
            }
        });
        isMyPublishPPT = getIntent().getBooleanExtra("isMyPublishPPT", true);
        if (isMyPublishPPT) {
            tv_title.setText(getResources().getString(R.string.ppt_string_18));
        } else {
            tv_title.setText(getResources().getString(R.string.ppt_string_17));
        }

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setItemPrefetchEnabled(false);
        recycle_content.setLayoutManager(manager);
        adapter = new PPTStoreAdapter(this, datas);
        recycle_content.setAdapter(adapter);

        swipe_refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refresh_layout.setEnableLoadmore(true);
                requestPPTStram();
            }
        });
        swipe_refresh_layout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                requestPPTStram();
            }
        });
        pptModel = new PPTModelImpl();
        is_refresh=true;
        requestPPTStram();
    }

    private void requestPPTStram() {
        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }
        pptModel.RequestMyPublishStorePPTStream(local_page, isMyPublishPPT).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
            }
        }).subscribe(new CommonSubscriber<PPTBean>() {
            @Override
            public void onSuccess(PPTBean response) {
//                hideProgress();
                swipe_refresh_layout.finishRefresh();
                swipe_refresh_layout.finishLoadmore();
                if ((response.getDataList() != null) && (response.getDataList().size() >= 0)) {
                    swipe_refresh_layout.finishRefresh();
                    swipe_refresh_layout.finishLoadmore();
                    if (is_refresh) {
                        datas.clear();
                        page_index = 1;
                    }
                    datas.addAll(response.getDataList());
                    dealDatas();
                    adapter.setDatas(datas);
                    adapter.notifyDataSetChanged();
                    page_index++;
                } else {
                    swipe_refresh_layout.finishLoadmore();
                    swipe_refresh_layout.setEnableLoadmore(false);
                }
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                swipe_refresh_layout.finishRefresh();
            }
        });
    }

    private void dealDatas() {
        if (datas != null) {
            for (PPTEntity entity : datas) {
                entity.setStoredPublish(true);
                entity.setMyPublish(isMyPublishPPT);
            }
        }
    }

}
